package com.fjwt.gz.proxy.ctrl.award.virtual;


import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.fjwt.gz.core.model.ApiRes;
import com.fjwt.gz.db.dto.AwardVirtualInsideDTO;
import com.fjwt.gz.db.entity.AwardVirtualInsideEntity;
import com.fjwt.gz.db.vo.AwardVirtualInsideVO;
import com.fjwt.gz.proxy.ctrl.CommonCtrl;
import com.fjwt.gz.service.impl.AwardVirtualInsideService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


/**
 * 内部商品管理
 *
 * @Author: 梅亚锋
 * @Date: 2025-02-25
 */
@RestController
@Slf4j
@RequestMapping("/api/virtualGoods/inside")
public class AwardVirtualInsideController extends CommonCtrl {

    @Autowired
    private AwardVirtualInsideService virtualGoodsInsideService;


    /**
     * 内部商品列表
     *
     * @return
     */
    @GetMapping
    public ApiRes list() {
        AwardVirtualInsideDTO awardVirtualInsideDTO = getObject(AwardVirtualInsideDTO.class);
        log.info("机构端内部商品列表，请求地址：/api/virtualGoods/inside，请求参数：{}", JSONUtil.toJsonStr(awardVirtualInsideDTO));
        IPage<AwardVirtualInsideVO> page = virtualGoodsInsideService.queryList(getIPage(true), awardVirtualInsideDTO);
        log.info("机构端内部商品列表，请求地址：/api/virtualGoods/inside，返回结果：{}", JSONUtil.toJsonStr(page));
        return ApiRes.page(page);
    }

    /**
     * 获取内部商品
     * @return
     */
    @GetMapping("/getList")
    public ApiRes get() {
        AwardVirtualInsideDTO awardVirtualInsideDTO = getObject(AwardVirtualInsideDTO.class);
        log.info("机构端获取内部商品，请求地址：/api/virtualGoods/inside/get，请求参数：{}", JSONUtil.toJsonStr(awardVirtualInsideDTO));
        List<AwardVirtualInsideEntity> list = virtualGoodsInsideService.getList(awardVirtualInsideDTO);
        return ApiRes.ok(list);
    }


    /**
     * 修改内部商品
     * @return
     */
    // @PreAuthorize("hasAnyAuthority('ENT_VIRTUAL_GOODS_INSIDE_EDIT')")
    @PostMapping("/update")
    public ApiRes update() {
        AwardVirtualInsideDTO awardVirtualInsideDTO = getObject(AwardVirtualInsideDTO.class);
        log.info("机构端修改内部商品详情，请求地址：/api/virtualGoods/inside/update，请求参数：{}", JSONUtil.toJsonStr(awardVirtualInsideDTO));
        virtualGoodsInsideService.updateVirtualInside(awardVirtualInsideDTO);
        return ApiRes.ok();
    }

    /**
     * 划拨功能回显
     * @return
     */
    @PostMapping("/getCdkDetail")
    public ApiRes getCdkDetail() {
        String virtualInsideId = getValString("virtualInsideId");
        log.info("机构端内部商品划拨功能回显，请求地址：/api/virtualGoods/inside/getCdkDetail，请求参数：{}", virtualInsideId);
        JSONObject result= virtualGoodsInsideService.getCdkDetail(virtualInsideId);
        log.info("机构端内部商品划拨功能回显，请求地址：/api/virtualGoods/inside/getCdkDetail，返回结果：{}", JSONUtil.toJsonStr(result));
        return ApiRes.ok(result);
    }

    /**
     * 保存划拨cdk数量
     * @return
     */
    @PostMapping("/saveCdkNum")
    public ApiRes saveCdkNum() {
        AwardVirtualInsideDTO insideDTO = getObject(AwardVirtualInsideDTO.class);
        log.info("机构端内部商品保存划拨cdk数量，请求地址：/api/virtualGoods/inside/saveCdkNum，请求参数：{}", JSONUtil.toJsonStr(insideDTO));
        virtualGoodsInsideService.saveCdkNum(insideDTO);
        return ApiRes.ok();
    }


}
